package com.bocloud.cmp.driver.providers.jd;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.SubnetDriver;
import com.bocloud.cmp.model.SubnetModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.jcloud.cap.client.ClientConfig;
import com.jcloud.cap.client.IClient;
import com.jcloud.cap.client.JCloudClient;
import com.jcloud.cap.client.builder.v1.CreateSubnetRequestBuilder;
import com.jcloud.cap.client.builder.v1.DeleteSubnetRequestBuilder;
import com.jcloud.cap.client.builder.v1.QuerySubnetsRequestBuilder;
import com.jcloud.cap.client.exception.ClientException;
import com.jcloud.cap.client.exception.ServerException;
import com.jcloud.cap.client.http.HttpConfig;
import com.jcloud.cap.data.model.v1.CreateSubnetResponse;
import com.jcloud.cap.data.model.v1.DeleteSubnetResponse;
import com.jcloud.cap.data.model.v1.QuerySubnetsResponse;
import com.jcloud.cap.data.model.v1.QuerySubnetsResponse.Subnet;

public class JdSubnetDriver extends JdDriver implements SubnetDriver {
	private static Logger logger = LoggerFactory.getLogger(JdSubnetDriver.class);
	private IClient client;

	/**
	 * @param accessKey
	 * @param secretKey
	 * @param endpoint
	 */
	public JdSubnetDriver(String accessKey, String secretKey, String endpoint, String region) {
		super(accessKey, secretKey, endpoint, region);
		ClientConfig config = new ClientConfig();
		HttpConfig httpConfig = new HttpConfig();
		// 调用地址
		httpConfig.setEndpoint(endpoint);
		httpConfig.setReadTimeout(20000);
		httpConfig.setConnectTimeout(20000);
		config.setHttpConfig(httpConfig);
		config.setCredential(this.getCredential().getCapCredential());
		client = new JCloudClient(config);
	}

	@Override
	public BsmResult list() {
		QuerySubnetsRequestBuilder builder = QuerySubnetsRequestBuilder.create();
		this.init(builder, "network", "querySubnets", this.getRegion());
		try {
			QuerySubnetsResponse response = client.send(builder.build());
			List<SubnetModel> models = Lists.newArrayList();
			List<Subnet> subnets = response.getSubnets();
			if (null == subnets || subnets.isEmpty()) {
				return new BsmResult(true, models, "success");
			}
			for (Subnet subnet : subnets) {
				SubnetModel model = convert(subnet);
				models.add(model);
			}
			return new BsmResult(true, models, "success");
		} catch (ClientException | ServerException e) {
			logger.error("List subnet [{}] exception!", e);
			return new BsmResult(false, "List subnet failed");
		}

	}

	@Override
	public BsmResult create(SubnetModel subnet) {
		CreateSubnetRequestBuilder builder = CreateSubnetRequestBuilder.create();
		this.init(builder, "network", "createSubnet", this.getRegion());
		try {
			CreateSubnetResponse response = client.send(
					builder.subnetName(subnet.getName()).cidr(subnet.getCidr()).routerId(subnet.getRouterId()).build());
			return detail(response.getSubnetId());
		} catch (ClientException | ServerException e) {
			logger.error("Create subnet [{}] exception!", e);
			return new BsmResult(false, "Create subnet failed");
		}
	}

	@Override
	public BsmResult remove(String subnetId) {
		DeleteSubnetRequestBuilder builder = DeleteSubnetRequestBuilder.create();
		this.init(builder, "network", "deleteSubnet", this.getRegion());
		try {
			DeleteSubnetResponse response = client.send(builder.subnetId(subnetId).build());
			return new BsmResult(true, response.getRequestId(), "success");
		} catch (ClientException | ServerException e) {
			logger.error("Reomve subnet [{}] exception!", e);
			return new BsmResult(false, "Reomve subnet failed");
		}
	}

	@Override
	public BsmResult detail(String subnetId) {
		return detail(subnetId, null);
	}

	private BsmResult detail(String subnetId, String requestId) {
		QuerySubnetsRequestBuilder builder = QuerySubnetsRequestBuilder.create();
		this.init(builder, "network", "querySubnets", this.getRegion());
		try {
			QuerySubnetsResponse response = client.send(builder.build());
			SubnetModel model = null;
			List<Subnet> subnets = response.getSubnets();
			if (null == subnets || subnets.isEmpty()) {
				return new BsmResult(false, "该对象不存在");
			}
			for (Subnet subnet : subnets) {
				if (subnet.getSubnetId().equals(subnetId)) {
					model = convert(subnet);
					break;
				}
			}
			if (null == model) {
				return new BsmResult(false, "该对象不存在");
			}
			return new BsmResult(true, model, "success");
		} catch (ClientException | ServerException e) {
			logger.error("Query subnet [{}] exception!", e);
			return new BsmResult(false, "Query subnet failed");
		}
	}

	private SubnetModel convert(Subnet subnet) {
		SubnetModel model = new SubnetModel();
		model.setName(subnet.getRouterName());
		model.setCidr(subnet.getCidr());
		model.setSubnetId(subnet.getSubnetId());
		model.setNetworkId(subnet.getNetworkId());
		return model;
	}
}
